package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.SysProject;
import com.ruoyi.system.mapper.SysProjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.SysCheckBo;
import com.ruoyi.system.domain.vo.SysCheckVo;
import com.ruoyi.system.domain.SysCheck;
import com.ruoyi.system.mapper.SysCheckMapper;
import com.ruoyi.system.service.ISysCheckService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 审核记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-17
 */
@RequiredArgsConstructor
@Service
public class SysCheckServiceImpl implements ISysCheckService {

    private final SysCheckMapper baseMapper;

    @Autowired
    private SysProjectMapper sysProjectMapper;

    /**
     * 查询审核记录
     *
     * @param id 审核记录主键
     * @return 审核记录
     */
    @Override
    public SysCheckVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询审核记录列表
     *
     * @param bo 审核记录
     * @return 审核记录
     */
    @Override
    public TableDataInfo<SysCheckVo> queryPageList(SysCheckBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysCheck> lqw = buildQueryWrapper(bo);
        Page<SysCheckVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询审核记录列表
     *
     * @param bo 审核记录
     * @return 审核记录
     */
    @Override
    public List<SysCheckVo> queryList(SysCheckBo bo) {
        LambdaQueryWrapper<SysCheck> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysCheck> buildQueryWrapper(SysCheckBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysCheck> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProjectId() != null, SysCheck::getProjectId, bo.getProjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysCheck::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增审核记录
     *
     * @param bo 审核记录
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysCheckBo bo) {
        SysCheck add = BeanUtil.toBean(bo, SysCheck.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改审核记录
     *
     * @param bo 审核记录
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysCheckBo bo) {
        SysCheck update = BeanUtil.toBean(bo, SysCheck.class);
        validEntityBeforeSave(update);
        String status = bo.getStatus();
        Long projectId = bo.getProjectId();
        SysProject sysProject = sysProjectMapper.selectById(projectId);
        if (Constants.SYS_CHECK_STATUS_YTG.equals(status) ){
            sysProject.setStatus(Constants.SYS_PROJECT_STATUS_YSH);
        } else if (Constants.SYS_CHECK_STATUS_DSH.equals(status) || Constants.SYS_CHECK_STATUS_WTG.equals(status)) {
            sysProject.setStatus(Constants.SYS_PROJECT_STATUS_DSH);
        }
        sysProjectMapper.updateById(sysProject);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysCheck entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除审核记录
     *
     * @param ids 需要删除的审核记录主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
